
//Table A6 final run

log using "$rootpath/log/Table_A6_Results.log", replace
eststo clear
use "$rootpath/processed_data/Estimation_Data.dta", clear
drop if key_estimation_sample==0
sort state_number
gen temp_id=facility_id if post==0
egen num_facil_temp=nvals(temp_id), by(state_number)
egen num_facilities=max(num_facil_temp), by(state_number)
sort facility_id year
egen years_observed=nvals(year), by(facility_id)

///This is definition zero.
//Note definition zero is mandatory to the extent that we want to show how we define the matrix codes doesn't matter. 
drop treatXpost
gen treatXpost=avg_old_code_prob2*post

reghdfe  log_damage55 treatXpost if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store damage_d1

///This is emissions definition zero 
reghdfe  log_emissions4 treatXpost if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store emission_d1
 
///This is definition two
drop treatXpost
gen treatXpost=(state_delisted_hpvs/num_facilities)*post
replace treatXpost=0 if treatXpost==.
 
reghdfe  log_damage55 treatXpost if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store damage_d2


///This is for emissions with definition two. 
reghdfe   log_emissions4  treatXpost if key_estimation_sample==1, absorb(facility_id n5#year) vce(cluster facility_id)
estimates store emission_d2
  
esttab damage_d1 damage_d2 emission_d1 emission_d2 using "$rootpath/table_graph/TBL_A6.tex",  star( * 0.1 ** 0.05 *** 0.01) cells(b(star fmt(3)) se(par fmt(3))) keep(treatXpost _cons) scalars(N F) ar2 replace 
log close
